﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using MaymuseMS.Models;

namespace MaymuseMS.DAL {
    public class MaymuseMSInitializer : DropCreateDatabaseIfModelChanges<MaymuseMSContext> {
        protected override void Seed(MaymuseMSContext context) {
            var parts = new List<Parts>{
                new Parts{ Number="lbj001", Name="零部件1" , Model="大", Spec="超大", Unit="个"},
                new Parts{ Number="lbj002", Name="零部件2" , Model="", Spec="", Unit="分"},
                new Parts{ Number="lbj003", Name="零部件3" , Model="", Spec="", Unit="吨"}
            };
            parts.ForEach(p => context.Parts.Add(p));

            var semifinisheds = new List<Semifinished>{
                new Semifinished{ Number="bcp001", Name="半成品1" , Model="大", Spec="超大", Unit="个"},
                new Semifinished{ Number="bcp002", Name="半成品2" , Model="", Spec="", Unit="分"},
                new Semifinished{ Number="bcp003", Name="半成品3" , Model="小", Spec="合适", Unit="吨"}
            };
            semifinisheds.ForEach(p => context.Semifinisheds.Add(p));

            var finisheds = new List<Finished>{
                new Finished{ Number="cp001", Name="成品1" , Model="大", Unit="个"},
                new Finished{ Number="cp002", Name="成品2" , Model="", Unit="分"},
                new Finished{ Number="cp003", Name="成品3" , Model="中", Unit="吨"}
            };
            finisheds.ForEach(p => context.Finisheds.Add(p));

            var configurations = new List<Configuration>{
                new Configuration{ Name="配置单1", Product= semifinisheds[0], Remark="备注"},
                new Configuration{ Name="配置单2", Product= semifinisheds[1]},
                new Configuration{ Name="配置单3", Product= finisheds[2], Remark="备注"}
            };
            configurations.ForEach(p => context.Configurations.Add(p));

            var configurationItems = new List<ConfigurationItem>{
                new ConfigurationItem{ Configuration= configurations[0], Product=parts[0], Count=10 },
                new ConfigurationItem{ Configuration= configurations[0], Product=parts[1], Count=20 },
                new ConfigurationItem{ Configuration= configurations[0], Product=parts[2], Count=30 },
                new ConfigurationItem{ Configuration= configurations[1], Product=parts[0], Count=12 },
                new ConfigurationItem{ Configuration= configurations[1], Product=parts[2], Count=13 },
                new ConfigurationItem{ Configuration= configurations[2], Product=parts[1], Count=23 }
            };
            configurationItems.ForEach(p => context.ConfigurationItems.Add(p));

            var suppliers = new List<Supplier>{
                new Supplier{ Number="供应商编号1111", Name="供应商1", Remark="王总去洗澡了", WebSiteUrl="www.sina.com.cn",
                    Contacts = new Models.ValueObject.Contacts{ Address="光华路", Fax="8888888"},
                    Banks = new Models.ValueObject.Bank{ Account="很长的银行账户1", Name="开户银行1"}
                },
                new Supplier{ Number="供应商编号2222", Name="供应商2",
                    Contacts = new Models.ValueObject.Contacts{ Phone="88888888", Fax="8888888"},
                    Banks = new Models.ValueObject.Bank{ Name="开户银行2", Number="银行行号2"}
                },
                new Supplier{ Number="供应商编号3333", Name="供应商3", Remark="酒吧开张去看看",
                    Contacts = new Models.ValueObject.Contacts{ Address="飞龙路飞龙路飞龙路飞龙路飞龙路飞龙路飞龙路飞龙路", Phone="88888888" },
                    Banks = new Models.ValueObject.Bank{ Account="很长的银行账户3", Number="银行行号3"}
                }
            };
            suppliers.ForEach(p => context.Suppliers.Add(p));

            var supplierUsers = new List<SupplierUser>{
                new SupplierUser{ Name="联系人1", Post="经理", Supplier=suppliers[0]},
                new SupplierUser{ Name="联系人2", Telephone="123456789", Supplier=suppliers[0]},
                new SupplierUser{ Name="联系人3", Email="11@111.com", Supplier=suppliers[1]}
            };
            supplierUsers.ForEach(s=>context.SupplierUsers.Add(s));

            var productItems = new List<ProductItem>{
                new ProductItem{ Supplier=suppliers[0], Product=parts[0], Remark="备注"},
                new ProductItem{ Supplier=suppliers[0], Product=parts[1], Remark="备注"},
                new ProductItem{ Supplier=suppliers[0], Product=parts[1], Remark="备注"},
                new ProductItem{ Supplier=suppliers[1], Product=semifinisheds[1], Remark="备注"},
                new ProductItem{ Supplier=suppliers[1], Product=semifinisheds[2], Remark="备注"},
                new ProductItem{ Supplier=suppliers[2], Product=semifinisheds[1], Remark="备注"}
            };
            productItems.ForEach(s => context.ProductItems.Add(s));

            var orders = new List<Order>{
                new Order{ Number="采购排产单编号1", Date=DateTime.Parse("1999-1-1"), 
                    DeliveryDate=DateTime.Parse("2011-1-1"),
                    Supplier=suppliers[0], SupplierUser=supplierUsers[0] }
            };
            orders.ForEach(o => context.Orders.Add(o));
            context.SaveChanges();
        }
    }
}